package jiyici;

import handler.DataInvalidException;

import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonNode;

public class Server2ClientSyncData {

    private ArrayList<Server2ClientSyncItem> items;

    public ArrayList<Server2ClientSyncItem> getItems() {
        return items;
    }

    public void fromJson(JsonNode json) throws DataInvalidException {
        if (json == null || !json.isArray()) {
            throw new DataInvalidException();
        }
        int size = json.size();
        items = new ArrayList<Server2ClientSyncItem>(size + 1);
        for (int i = 0; i < size; i++) {
            JsonNode itemJson = json.get(i);
            String tableName = itemJson.findPath("tableName").getTextValue();
            if (StringUtils.isBlank(tableName))
                throw new DataInvalidException();
            long lastmodified = itemJson.findPath("lastmodified").getLongValue();
            items.add(new Server2ClientSyncItem(tableName, lastmodified));
        }
    }
}
